import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @version Create on 2012-10-29
 * @author Yinzi Chen
 */

public class Anagrams {

    public ArrayList<String> anagrams(String[] strs) {
        ArrayList<String> res = new ArrayList<String>();
        int n = strs.length;
        int root[] = new int[n];
        int cnt[] = new int[n];
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < n; ++i) {
            char c[] = strs[i].toCharArray();
            Arrays.sort(c);
            String s = new String(c);
            Integer p = map.get(s);
            if (p != null) {
                root[i] = p;
                cnt[p]++;
                continue;
            } else {
                map.put(s, i);
                root[i] = i;
                cnt[i]++;
            }
        }
        for (int i = 0; i < n; ++i) {
            if (cnt[root[i]] > 1)
                res.add(strs[i]);
        }
        return res;
    }

}
